/**
 * BasicFileView.java
 * @author: liuex
 * 2007-12-24 ����03:05:13
 *
 * note: 
 */
package org.lex.io.filesystem;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.filechooser.FileView;

public class CacheFileView extends FileView {
	private Map<File, Icon> iconCache = null;
	private FileView fileview = null;

	public static CacheFileView wrap(FileView fileview) {
		return new CacheFileView(fileview);
	}

	private CacheFileView(FileView fileview) {
		this.iconCache = new HashMap<File, Icon>();
		this.fileview = fileview;
	}

	public void clearIconCache() {
		iconCache = new HashMap<File, Icon>();
	}

	public String getName(File f) {
		return fileview.getName(f);
	}

	public String getDescription(File f) {
		return fileview.getDescription(f);
	}

	public String getTypeDescription(File f) {
		return fileview.getTypeDescription(f);
	}

	public Icon getIcon(File f) {
		Icon icon = getCachedIcon(f);
		if (icon != null) {
			return icon;
		} else {
			icon = fileview.getIcon(f);
			if (null != icon)
				cacheIcon(f, icon);
			return icon;
		}
	}

	private Icon getCachedIcon(File f) {
		return iconCache.get(f);
	}

	private void cacheIcon(File f, Icon i) {
		if (f == null || i == null) {
			return;
		}
		iconCache.put(f, i);
	}
}
